package com.baidu.aihsp.algoservice.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.aihsp.algoservice.entity.Algo;
import com.baidu.aihsp.algoservice.service.AlgoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author wenchang
 * @create 2020-08-25-12:02
 */
@RestController
@RequestMapping("/algo")
public class AlgoController {

    @Autowired
    AlgoService algoService;

    /** 修改 */
    @PostMapping("/save")
    public Algo save(@RequestBody @Valid final Algo algo){
        return algoService.save(algo);
    }

    /** 增加 */
    @PostMapping("/add")
    public Algo add(@RequestBody @Valid final Algo algo){
        return algoService.add(algo);
    }

    @GetMapping("/list")
    public Page<Algo> getList(@PageableDefault(page = 0,size = 10,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable){
        return algoService.getList(pageable);
    }

    @GetMapping("/detail")
    public JSONObject getDetail(Long algoId){
        String a= JSON.toJSONString(algoService.getOne(algoId));
        return JSONObject.parseObject(a);
    }

    @PostMapping("/delete")
    public void delete(Long algoId){
        algoService.deleteById(algoId);
    }
}
